package com.hunan.api.catv.service.biz.system;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.hunan.api.catv.entity.ServiceResult;
import com.hunan.api.catv.entity.po.SystemAuth;
import com.hunan.api.catv.entity.vo.SystemAuthVo;
import com.hunan.api.catv.service.ResultService;
import com.hunan.api.catv.service.impl.system.SystemAuthService;
import com.hunan.api.catv.utils.GsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.List;

@CacheConfig(cacheNames = "SystemAuthBiz")
@Service
public class SystemAuthBiz extends ResultService {

    @Autowired
    SystemAuthService systemAuthService;

    @Autowired
    SystemRoleAuthBiz systemRoleAuthBiz;

    @Autowired
    Gson gson;

    public PageInfo<SystemAuthVo> page(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        SystemAuth systemAuth = new SystemAuth();
        List<SystemAuthVo> list = systemAuthService.list(systemAuth);
        return new PageInfo<SystemAuthVo>(list);
    }

    public SystemAuthVo info(Integer id) {
        return systemAuthService.getById(id);
    }

    public ServiceResult add(SystemAuth systemAuth) {
        return systemAuthService.add(systemAuth);
    }

    public ServiceResult edit(SystemAuth systemAuth) {
        if (ObjectUtils.isEmpty(systemAuth.getSysAuthId())) {
            return buildFail("", "id not null");
        }
        return systemAuthService.update(systemAuth);
    }

    public JsonArray authList(Integer roleId) {
        SystemAuth sysAuthPo = new SystemAuth();
        sysAuthPo.setLevel(1);
        sysAuthPo.setSysAuthPid(0);
        sysAuthPo.setSysRoleId(roleId);
        List<SystemAuthVo> firstSystemAuthList = systemAuthService.listAuth(sysAuthPo);
        JsonArray array = new JsonArray();
        firstSystemAuthList.forEach(sysAuth -> {
            JsonObject menu = new JsonObject();
            menu.addProperty("path", sysAuth.getSysAuthAction());
            menu.addProperty("component", "layout");
            menu.addProperty("name", sysAuth.getSysAuthName());
            menu.addProperty("alwaysShow", true);
            menu.addProperty("hidden", sysAuth.getHidden() == 0 ? true : false);
            JsonObject meta = new JsonObject();
            meta.addProperty("title", sysAuth.getSysAuthName());
            meta.addProperty("icon", sysAuth.getIcon());
            menu.add("meta", meta);
            menu.addProperty("breadcrumb", true);
            menu.addProperty("border", sysAuth.getBorder() == 0 ? false : true);
            SystemAuth sysAuthPo1 = new SystemAuth();
            sysAuthPo1.setLevel(2);
            sysAuthPo1.setSysAuthPid(sysAuth.getSysAuthId());
            sysAuthPo1.setSysRoleId(roleId);
            List<SystemAuthVo> senSystemAuthList = systemAuthService.listAuth(sysAuthPo1);
            if (!ObjectUtils.isEmpty(senSystemAuthList)) {
                menu.addProperty("redirect", senSystemAuthList.get(0).getSysAuthAction());
                JsonArray children = new JsonArray();
                senSystemAuthList.forEach(sen -> {
                    JsonObject senMenu = new JsonObject();
                    senMenu.addProperty("path", sen.getSysAuthAction());
                    //senMenu.put("component", sen.getSysAuthAction());
                    if (sen.getSysAuthAction().indexOf(":") == -1) {
                        senMenu.addProperty("component", sen.getSysAuthAction());
                    }
                    if (sen.getSysAuthAction().indexOf(":") != -1) {
                        senMenu.addProperty("component", sen.getSysAuthAction().substring(0, sen.getSysAuthAction().indexOf(":") - 1));
                    }
                    senMenu.addProperty("name", (sen.getSysAuthAction().replaceAll("/", "")).replace(":", ""));
                    senMenu.addProperty("hidden", sen.getHidden() == 0 ? true : false);
                    JsonObject senMeta = new JsonObject();
                    senMeta.addProperty("title", sen.getSysAuthName());
                    senMenu.add("meta", senMeta);
                    children.add(senMenu);
                });
                menu.add("children", children);
            }
            array.add(menu);
        });
        String json = "{\"path\": \"*\",\"component\": \"layout\",\"redirect\": \"/404\","
                + "\"name\": \"NotFound\",\"alwaysShow\": true,\"hidden\": true,"
                + "\"children\": [{\"path\": \"/404\",\"component\": \"/404\",\"name\": \"404\","
                + "\"hidden\": true,\"meta\": {\"title\": \"404\"}}]}";
        JsonObject menu = GsonUtils.fromJson(json, JsonObject.class);
        array.add(menu);
        return array;
    }

    public JsonObject listAll(Integer sysRoleId) {
        JsonObject result = new JsonObject();
        SystemAuth systemAuth = new SystemAuth();
        systemAuth.setLevel(1);            //一级管理员
        systemAuth.setSysAuthPid(0);    //没有上层权限
        List<SystemAuthVo> fList = systemAuthService.list(systemAuth);
        List<Integer> listPid = new ArrayList<>();
        if (!ObjectUtils.isEmpty(fList)) {
            for (SystemAuthVo systemAuthVo : fList) {
                SystemAuth sysAuthPo1 = new SystemAuth();
                sysAuthPo1.setLevel(2);
                sysAuthPo1.setSysAuthPid(systemAuthVo.getSysAuthId());
                List<SystemAuthVo> sList = systemAuthService.list(sysAuthPo1);
                //要删除夫级id 这样前端直接拿到子id 做回显
                listPid.add(systemAuthVo.getSysAuthId());
                systemAuthVo.setChildren(sList);
            }
        }
        List<Integer> roleAuthList = systemRoleAuthBiz.list(sysRoleId);
        roleAuthList.removeAll(listPid);
        JsonArray auth = new JsonParser().parse(GsonUtils.toJson(fList, List.class)).getAsJsonArray();
        result.add("authList", auth);
        JsonArray roleAuth = new JsonParser().parse(GsonUtils.toJson(roleAuthList, List.class)).getAsJsonArray();
        result.add("roleAuthList", roleAuth);
        return result;
    }


}